// pages/order/index.js
import Toast from 'tdesign-miniprogram/toast/index';

const app = getApp()
const wxpay = require('../../utils/pay.js')
const dateUtil = require('../../utils/date.js')
const CONFIG = require('../../config.js')
const WXAPI = require('../../wxapi/main.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    settlmentList: [],
    baseRefresh: {
      value: false,
    },
    loadingProps: {
      size: '50rpx',
    },
    backTopVisible: false,
    current_no: 0,
    isLastPage: false,
    page_size: 10,
    paymentVenueMap: {
      "1": "开通会员",
      "2": "预约",
      "3": "机位结算",
      "4": "余额充值"
    },
    paymentVenuePicMap: {
      "1": "https://7072-prod-8g7u9tmqac56ab70-1318102458.tcb.qcloud.la/%E5%BE%AE%E4%BF%A1%E5%9B%BE%E7%89%87_20230524105402.jpg?sign=4df805a55821f469e53bb2e27d4dd82f&t=1684899285",
      "2": "https://7072-prod-8g7u9tmqac56ab70-1318102458.tcb.qcloud.la/%E5%BE%AE%E4%BF%A1%E5%9B%BE%E7%89%87_20230524105410.jpg?sign=5ce8dbab4bcd27bc63b22f989224abf3&t=1684899385",
      "3": "https://7072-prod-8g7u9tmqac56ab70-1318102458.tcb.qcloud.la/%E5%BE%AE%E4%BF%A1%E5%9B%BE%E7%89%87_20230524105405.jpg?sign=034ab7ca8e8190d2af6b4a08e643db1c&t=1684899371"
    },
    statusMap: {
      '0': '初始化',
      '1': '未付款',
      "2":"已支付",
      "3":"已取消",
      "4":"发起退款",
      "5":"已退款",
    },
    userInfo:{},
    interval:null,
  },
  goSettlementDetail(e) {

    wx.navigateTo({
      url: "/pages/settlement/index?order_no=" + e.currentTarget.dataset.data.order_no
    })
  },
  onPullDownRefresh() {
    
    this.setData({
      current_no:0,
      settlmentList:[],
      isLastPage:false
    })
    this.queryWaitSettlement( () => {
      this.setData({
        'baseRefresh.value': false
      })
    });

  },
  onScroll(e) {
    const { scrollTop } = e.detail;

    this.setData({
      backTopVisible: scrollTop > 100,
    });
  },

  queryWaitSettlement(callback) {
    if (this.data.isLastPage) {
      wx.showToast({
        title: '没有更多的数据'
      })
      return;
    }
    
    var userInfo = wx.getStorageSync('userInfo');
    this.setData({
      isLastPage: false,
      userInfo
    })
    var { current_no, page_size } = this.data;
    var param = {
      current_no,
      page_size
    }

    
    WXAPI.queryWaitSettlement(param).then(res=> {
      var now=new Date().getTime();
      var settlmentList = res;
      settlmentList.forEach(item => {
        item.close_time=new Date(item.expire_time).getTime()-now;
        
        item.order_time = dateUtil.toDate(item.order_time);
        item.payment_time = dateUtil.toDate(item.payment_time);
      })
      this.setData({
        settlmentList: settlmentList,
      });
      if (callback) {
        callback()
      }
      wx.hideNavigationBarLoading();
    }).catch((e) => {
      wx.hideNavigationBarLoading();
    });
  },
  //触底刷新
  loadMoreData() {
    if (this.data.isLastPage) {
      return;
    }
    var user_id = wx.getStorageSync('user_id');
    var { current_no, page_size } = this.data;

    current_no = current_no + page_size;
    var param = {
      current_no,
      page_size
    }
    
    WXAPI.queryWaitSettlement(user_id, param).then(res=> {

      var settlmentList = res;

      if (settlmentList.length > 0) {
        settlmentList.forEach(item => {
          item.order_time = dateUtil.toDate(item.order_time);
          item.payment_time = dateUtil.toDate(item.payment_time);
        })
        var newList = this.data.settlmentList.concat(settlmentList);
        this.setData({
          settlmentList: newList,
          current_no
        });
      } else {
        this.setData({
          isLastPage: true,
        });
      }
      wx.hideNavigationBarLoading();
    }).catch((e) => {
      wx.hideNavigationBarLoading();
    });
  },
  refreshData(){
    this.setData({
      settlmentList:[],
      current_no: 0,
      page_size: 10,
      isLastPage:false
    })
    this.queryWaitSettlement();
     
  },

  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.refreshData()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    
   
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },


  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})